package com.huan.order.service.impl;

import com.huan.api.AccountFeignApi;
import com.huan.api.StorageFeignApi;
import com.huan.order.entity.SeataOrder;
import com.huan.order.mapper.OrderMapper;
import com.huan.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private StorageFeignApi storageFeignApi;
    @Autowired
    private AccountFeignApi accountFeignApi;

    @GlobalTransactional
    @Override
    public void createOrder(SeataOrder seataOrder) {
        log.info("-----> 创建新订单");
        seataOrder.setStatus(0);
        orderMapper.createOrder(seataOrder);
        log.info("-----> 创建新订单完成");

        log.info("-----> 开始减库存");
        storageFeignApi.decrease(seataOrder.getProductId(),seataOrder.getCount());
        log.info("-----> 结束减库存");

        log.info("-----> 开始减余额");
        accountFeignApi.decrease(seataOrder.getUserId(),seataOrder.getMoney());
        log.info("-----> 结束减余额");

        log.info("-----> 开始更新订单状态");
        seataOrder.setStatus(1);

        orderMapper.updateById(seataOrder);
        log.info("-----> 结束更新订单状态");
        throw new RuntimeException("hhh");

    }
}
